package com.easyschool.model.bo;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.easyschool.model.school.Class;
import com.easyschool.model.school.School;

public class BoClass {
	
	private static BoClass instance;
	private BoClass(){}
	
	public static BoClass getInstance(){
		if(instance == null) instance = new BoClass();
		return instance;
	}
	
	
	private static SessionFactory sessionFactory = null;
	public List<com.easyschool.model.school.Class> getAllClasses() throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				@SuppressWarnings("unchecked")
				List<com.easyschool.model.school.Class> list = (List<com.easyschool.model.school.Class>)session.createCriteria(com.easyschool.model.school.Class.class).list();
				
				tx.commit();
				
				return list;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public Class getClassById(Integer idClass) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				@SuppressWarnings("unchecked")
				Class theClass = (com.easyschool.model.school.Class)session.createCriteria(com.easyschool.model.school.Class.class).add(Restrictions.idEq(idClass)).uniqueResult();
				
				tx.commit();
				
				return theClass;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	
	
	public List<com.easyschool.model.school.Class> getAllClassesByFilters(String libelle, Integer schoolId) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				Criteria crit = session.createCriteria(com.easyschool.model.school.Class.class);
				
				if(libelle != null){
					crit.add( Restrictions.like("libelle", "%"+libelle+"%") );
				}
				if(schoolId != null){
					crit.add( Restrictions.eq("schoolId", schoolId) );
				}
				
				
				@SuppressWarnings("unchecked")
				List<com.easyschool.model.school.Class> list = crit.list();
				
				tx.commit();
				
				return list;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public Integer addClass(com.easyschool.model.school.Class theClass) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				Integer id = (Integer)session.save(theClass);
				
				tx.commit();
				
				return id;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public Integer removeClass(Integer id) throws Exception{
		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				session.delete(new Class(id));
				
				tx.commit();
				
				return id;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	
	}
	
	
	public List<com.easyschool.model.school.Class> getClassesBySchool(Integer schoolId) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				Criteria crit = session.createCriteria(com.easyschool.model.school.Class.class);
				
				crit.add( Restrictions.eq("schoolId", schoolId) );
				
				
				
				@SuppressWarnings("unchecked")
				List<com.easyschool.model.school.Class> list = crit.list();
				
				tx.commit();
				
				return list;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
}
